﻿#include "chargecardmanageinfo.h"
#include "ui_chargecardmanageinfo.h"

ChargeCardManageInfo::ChargeCardManageInfo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChargeCardManageInfo)
{
    ui->setupUi(this);
    ui->labelresult->hide();
    time = new QLabel(ui->frameBottom);
    time->setGeometry(10, 10, 200, 40);
    time->setStyleSheet("QLabel{font: 20px; color: rgb(25, 25, 112);}");
    signal = new QLabel(ui->frameBottom);
    signal->setGeometry(700, 18, 41, 22);
    signal->setStyleSheet("QLabel{background-color:transparent;}");

    QDir dir(IMAGE_PATH);
    if(dir.exists())
    {
        ui->frameTop->setStyleSheet(LOGO_STY);
    }

    LabelTree[0].Edit = ui->m_editCardA1;
    LabelTree[0].Bind = ui->buttonBinding1;
    LabelTree[0].UnBind = ui->buttonUnBinding1;

    LabelTree[1].Edit = ui->m_editCardA2;
    LabelTree[1].Bind = ui->buttonBinding2;
    LabelTree[1].UnBind = ui->buttonUnBinding2;

    LabelTree[2].Edit = ui->m_editCardB1;
    LabelTree[2].Bind = ui->buttonBinding3;
    LabelTree[2].UnBind = ui->buttonUnBinding3;

    LabelTree[3].Edit = ui->m_editCardB2;
    LabelTree[3].Bind = ui->buttonBinding4;
    LabelTree[3].UnBind = ui->buttonUnBinding4;

    LabelTree[4].Edit = ui->m_editCardB3;
    LabelTree[4].Bind = ui->buttonBinding5;
    LabelTree[4].UnBind = ui->buttonUnBinding5;

    for (int i=0; i<5; i++)
    {
        connect(LabelTree[i].Bind, SIGNAL(clicked()), this, SLOT(ButtonBind()));
        connect(LabelTree[i].UnBind, SIGNAL(clicked()), this, SLOT(ButtonUnBind()));
    }

    connect(stackLayout, SIGNAL(currentChanged(int)), this, SLOT(ButtonEnable(int)));
    qDebug()<<QString("到此为止");
    ReadTcucarddb();
}
void ChargeCardManageInfo::AdministrationCard_Binded()
{
    if(ui->m_editCardA1->text().isEmpty()&&ui->m_editCardA2->text().isEmpty())
    {
        stackLayout->setCurrentIndex(chargeCardManageInfoPage);
    }
    else
    {
        emit SwipingAdministrationCard(ReferBindedAdministrationCard, ui->m_editCardA1->text().trimmed(), ui->m_editCardA2->text().trimmed());
        stackLayout->setCurrentIndex(WaitSwipCardPage);
    }
}

void ChargeCardManageInfo::ButtonUnBind()
{
    /*待完成*/
    QPushButton* btn =  static_cast<QPushButton*>(sender());
    for(int i=6; i<11; i++)
    {
        if(btn == LabelTree[i-6].UnBind)
        {
            emit UnBindingCard(i/*6~10*/, LabelTree[i-6].Edit->text().trimmed(), ui->m_editCardA1->text().trimmed(), ui->m_editCardA2->text().trimmed());
            stackLayout->setCurrentIndex(WaitSwipCardPage);
            break;
        }
    }
}

void ChargeCardManageInfo::ButtonBind()
{
    /*发给主窗口一个信号告知启动刷卡线程*/
    /*待完成*/
    QPushButton* btn =  static_cast<QPushButton*>(sender());
    for(int i=1; i<6; i++)
    {
        if(btn == LabelTree[i-1].Bind)
        {
            emit BindingCard(i/*1~5*/, LabelTree[i-1].Edit, ui->m_editCardA1->text().trimmed(), ui->m_editCardA2->text().trimmed());
            stackLayout->setCurrentIndex(WaitSwipCardPage);
            break;
        }
    }
}

void ChargeCardManageInfo::LabelShow(int flag)
{
    if(flag == 0)
    {
        ui->labelresult->setText(tr("绑定失败"));
        ui->labelresult->show();
        QTimer::singleShot(5000,ui->labelresult,SLOT(hide()));//显示5秒后隐藏

    }
    else if(flag == 1)
    {
        ui->labelresult->setText(tr("绑定成功"));
        ui->labelresult->show();
        QTimer::singleShot(5000,ui->labelresult,SLOT(hide()));//显示5秒后隐藏

    }
    else if(flag == 2)
    {
        ui->labelresult->setText(tr("解绑失败"));
        ui->labelresult->show();
        QTimer::singleShot(5000,ui->labelresult,SLOT(hide()));//显示5秒后隐藏

    }
    else if(flag == 3)
    {
        ui->labelresult->setText(tr("解绑成功"));
        ui->labelresult->show();
        QTimer::singleShot(5000,ui->labelresult,SLOT(hide()));//显示5秒后隐藏

    }
}


void ChargeCardManageInfo::on_buttonBack_clicked()
{
    /*待完成*/
    stackLayout->setCurrentIndex(SystemSetting4Page);
}

void ChargeCardManageInfo::ButtonEnable( int current)
{
    ReadTcucarddb();
    if(current == chargeCardManageInfoPage)
    {
        for (int i = 0; i<5; i++)
        {
            if(LabelTree[i].Edit->text().isEmpty())
            {
                LabelTree[i].UnBind->setEnabled(false);
                LabelTree[i].Bind->setEnabled(true);
            }
            else
            {
                LabelTree[i].UnBind->setEnabled(true);
                LabelTree[i].Bind->setEnabled(false);
            }
        }
    }
}

void ChargeCardManageInfo::ReadTcucarddb()//读取tcucard.db数据库的内容
{

    for(int i=0; i<5; i++)
    {
        LabelTree[i].Edit->clear();
    }
    if( protocol_queryCard(mcard) )
    {
        return;
    }

    QSqlDatabase db;
    if(QSqlDatabase::contains("qt_sql_default_connection"))
    {
      db = QSqlDatabase::database("qt_sql_default_connection");
    }
    else
    {
      db = QSqlDatabase::addDatabase("QSQLITE");
    }

    db.setDatabaseName("/usr/local/sanway/dbTest/tcucard.db");
    bool ok = db.open();
    if(ok)
    {
        QSqlQuery query;
        query.exec("select * from tagCard");
        QString cardnum;
        QString cardtype;
        QString cardorder;
        int j = 0;
        while( query.next() )
        {
            cardnum = query.value(0).toString();
            cardtype = query.value(1).toString();
            cardorder = query.value(2).toString();

            if( query.isValid() )
            {
                if( ( cardtype == "a" )  && ( cardorder == "1" ) )
                {
                    ui->m_editCardA1->setText(cardnum);
                }

                if( ( cardtype == "a" )  && ( cardorder == "2" ) )
                {
                    ui->m_editCardA2->setText(cardnum);
                }

                if( ( cardtype == "b" )  && ( cardorder == "1" ) )
                {
                    ui->m_editCardB1->setText(cardnum); 
                }

                if( ( cardtype == "b" )  && ( cardorder == "2" ) )
                {
                    ui->m_editCardB2->setText(cardnum);
                }

                if( ( cardtype == "b" )  && ( cardorder == "3" ) )
                {
                    ui->m_editCardB3->setText(cardnum);
                }
                ++j;
            }
        }
    }
    db.close();

}

ChargeCardManageInfo::~ChargeCardManageInfo()
{
    delete ui;
}

void ChargeCardManageInfo::SendManagementtoCardAndQRcord(QString *sendNumcardinfo)
{
    for(int i=0; i<5; i++)
    {
        sendNumcardinfo[i] = LabelTree[i].Edit->text().trimmed();
    }
}
